import { Component, OnInit } from '@angular/core';
import { JsonApiService } from "app/core/api/json-api.service"; //http请求配置引入
import { Router, ActivatedRoute } from "@angular/router"; //路由引入
import { config } from '../../shared/smartadmin.config';

@Component({
	selector: 'sa-company',
	templateUrl: './company.component.html',
})
export class CompanyComponent implements OnInit {
	tableData: any = []; //table数据
	totalCount: number = 0; //分页总条数
	curPage: number = 1; //分页当前页
	pageSize: number = 10; //内置的分页每页条数
	searchKey: string = ''; //搜索keyword
	btnData: any = [];
	companyAdd: boolean = false;
	companyEdit: boolean = false;
	companyDeleteBatch: boolean = false;
	startTime: string = '';
	endTime: string = '';
	initDateRange: any = {
		startTime: '',
		endTime: '',
	}// 时间组件需要渲染的值
	businessTypeList: any[] = [] //行业类型
	businessType: string = ''

	constructor(private jsonApiService: JsonApiService, private router: Router, private activatedRoute: ActivatedRoute) {
		activatedRoute.queryParams.subscribe(queryParams => {
			this.curPage = queryParams.nowPage || 1;
		});
		this.businessTypeList = businessTypeList
	}

	ngOnInit() {
		this.initBtn();
		this.load();
	}
	// 接收子组件传递过来的数据 
	getSelectTableTime(time) {
		this.startTime = time.startTime;
		this.endTime = time.endTime;
	}
	//table刷新
	refresh() {
		this.load();
	}
	//权限按钮
	initBtn() {
		this.btnData = JSON.parse(decodeURIComponent(localStorage.getItem('cargpsw_system:company')));
		if (!this.btnData) { return };
		for (let item of this.btnData) {
			if (item.sourceKey === "company:add" && item.parentId) { this.companyAdd = true; }
			if (item.sourceKey === "company:edit" && item.parentId) { this.companyEdit = true; }
			if (item.sourceKey === "company:deleteBatch" && item.parentId) { this.companyDeleteBatch = true; }
		}
	}
	//表格搜索
	table_search(word) {
		this.curPage = 1
		this.searchKey = common.stringTrim(this.searchKey);
		this.load();
	}
	//初始化加载list数据
	load() {
		if (this.startTime && !this.endTime) {
			this.jsonApiService.message('请选择结束时间');
			return
		}
		if (!this.startTime && this.endTime) {
			this.jsonApiService.message('请选择开始时间');
			return
		}
		let data = {
			pageSize: this.pageSize,
			nowPage: this.curPage,
			parameters: {
				searchName: this.searchKey,
				startTime: this.startTime,
				endTime: this.endTime,
				businessType: this.businessType
			},
			isExport: false
		}
		let reqdata = 'gridPager=' + JSON.stringify(data);
		this.jsonApiService.http("company/list.html", reqdata).subscribe(data => {
			this.tableData = data.exhibitDatas;
			this.totalCount = data.recordCount;
		})
	}
	//导出
	exprotData() {
		if (this.startTime && !this.endTime) {
			this.jsonApiService.message('请选择结束时间');
			return
		}
		if (!this.startTime && this.endTime) {
			this.jsonApiService.message('请选择开始时间');
			return
		}
		let data = {
			pageSize: this.pageSize,
			nowPage: this.curPage,
			parameters: {
				searchName: this.searchKey,
				startTime: this.startTime,
				endTime: this.endTime,
				businessType: this.businessType
			},
			isExport: true
		}
		common.exprotDataFile(data, 'gridPager', config.BaseURL + 'company/list.html')
	}
	//分页回调
	paginate(e) {
		this.curPage = e.page + 1;
		this.pageSize = e.rows;
		this.load();
	}
	//新建公司
	new_company() {
		this.router.navigate(['/system/company/company-page'], { queryParams: { type: 'new' } });
	}
	//编辑页面
	updata_company(row) {
		this.router.navigate(['/system/company/company-page'], { queryParams: { id: row.id, type: 'updata', nowPage: this.curPage } });
	}
	//公司删除
	del_company(row) {
		this.jsonApiService.confirm_message("此操作将永久删除数据,确认要删除?").then(() => {
			let reqdata = 'ids=' + row.id;
			this.jsonApiService.http("company/deleteBatch.html", reqdata).subscribe(data => {
				this.jsonApiService.message(data.message);
				if (data.success) {
					this.load();
				}
			})
		})
	}
	//根据公司行业类型id获取行业类型中文
	getBusinessTypeText(businessType) {
		const item = businessTypeList.find(item => {
			return item.value === businessType
		})
		return item ? item.label : '--'
	}
	//行业类型选择
	businessTypeChange(e) {
		this.businessType = e
	}
}